<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改密码</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
    <script src="js/jquery-3.1.0.min.js"></script>
    <script src="layui/layui.js" charset="utf-8"></script>

    <style>
        .layui-input {
            width: 200px;
        }
    </style>
</head>
<body>
<form class="layui-form" action="">
    <!--<div class="layui-form-item">-->
    <!--<label class="layui-form-label">用户名：</label>-->
    <!--<div class="layui-input-block">-->
    <!--<input type="text" name="username" readonly="readonly" class="layui-input">-->
    <!--</div>-->
    <!--</div>-->
    <div class="layui-form-item">
        <label class="layui-form-label">原密码：</label>
        <div class="layui-input-block">
            <input type="password" name="password" id="password" lay-verify="password" placeholder="请输入密码"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">新密码：</label>
        <div class="layui-input-block">
            <input type="password" name="newpwd" id="newpwd" lay-verify="newpwd" lay-reqtext="密码很重要，不能为空！"
                   placeholder="请输入新密码"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">确认密码：</label>
        <div class="layui-input-block">
            <input type="password" name="checkpwd" id="checkpwd" lay-verify="checkpwd" lay-reqtext="请确认新密码！"
                   placeholder="请确认新密码"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="pwdForm">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script>
    layui.use(['form', 'layer'], function () {
        var form = layui.form
            , layer = layui.layer

        //自定义验证规则
        form.verify({
            password: function (value) {
                if (value == "" || value == null) {
                    return "请输入旧密码"
                }
            },
            newpwd: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ],

            checkpwd: function (value) {
                var pwd = $("#newpwd").val();
                if (!new RegExp(pwd).test(value)) {
                    return '两次输入的新密码不一致'
                }
            },
        });

        //监听提交
        form.on('submit(pwdForm)', function (data) {
            $.ajax({
                url: 'admin/modifyPass',
                method: 'post',
                async: true,
                //contentType: 'application/json',
                data: {
                    pass: $("#password").val(),
                    newpass: $("#newpwd").val()
                },
                dataType: 'JSON',
                success: function (res) {
                    if (res.code == '200') {
                        layer.msg(res.message, {icon: 1, time: 1000},function () {
                            //修改成功，重新登录
                            exit();
                        });
                    } else if (res.code == '201') {
                        layer.msg(res.message, {icon: 5, time: 1000});
                    }
                },
                error: function (data) {
                    layer.msg("服务器忙...", {icon: 5, time: 1000})
                }
            });
            return false;
        })

        function exit() {
            $.ajax({
                type:"get",
                dataType:"json",
                url:"admin/logOut",
                async: true,
                success: function(data) {
                    if(data.code==0) {
                        parent.location.href='login.html'
                    } else {
                        alert(data.msg);
                    }
                },
                error: function(msg) {
                    alert("服务器请求失败！");
                }
            });
        }
    });
</script>
</body>
</html>